/** create by liuhua at 2019年5月21日 下午7:13:08**/
package com.star.truffle.gateway.filter;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.server.ServerWebExchange;

import reactor.core.publisher.Mono;

public class RedirectGatewayFilter implements GatewayFilter, Ordered {

  private String url ;
  private HttpStatus httpStatus;
  
  public RedirectGatewayFilter(HttpStatus httpStatus, String url) {
    this.url = url;
    this.httpStatus = httpStatus;
  }
  
  @Override
  public int getOrder() {
    return -100;
  }

  @Override
  public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    ServerHttpResponse response = exchange.getResponse();
    response.setStatusCode(httpStatus);
    response.getHeaders().set("Location", url);
    return exchange.getResponse().setComplete();
  }

}
